#include "vcd-other-topbar.h"

#include "../../vcd-message-dialog.h"
#include "../../../vcd-common-api/vcd-conf/vcd-conf.h"
#include "../../../vcd-common-api/vcd-common.h"

#define LABEL_POSITION "位置 :"
#define LABEL_LEFT     "左"
#define LABEL_TOP      "上"
#define LABEL_RIGHT    "右"

#define LABEL_SHOW_TEXT "位置 :"
#define LABEL_SHOW      "显示"
#define LABEL_HIDE      "隐藏"

#define LABEL_TIPS_SHOW_HIDE0 "温馨提示: "
#define LABEL_TIPS_SHOW_HIDE1 " 进入桌面后可使用 Ctrl_Shift_F12 显示/隐藏悬浮框"

typedef struct _st_topbar{
    GtkWidget* radio1;
    GtkWidget* radio2;
    GtkWidget* radio3;

    // show/hide
    GtkWidget* radio4;
    GtkWidget* radio5;
}StTopbar;

static void btn_toggled(GtkCheckButton *check_btn,gpointer priv)
{

    if(!g_vcd_conf) return;

    gboolean bres = gtk_toggle_button_get_active((GtkToggleButton *)check_btn);

    if(bres){
        const char* text = gtk_button_get_label((GtkButton*)check_btn);

        if(g_strcmp0(text,LABEL_LEFT) == 0)
            g_vcd_conf->topbar_position = POSITION_LEFT;
        else if(g_strcmp0(text,LABEL_TOP) == 0)
            g_vcd_conf->topbar_position = POSITION_TOP;
        else if(g_strcmp0(text,LABEL_RIGHT) == 0)
            g_vcd_conf->topbar_position = POSITION_RIGHT;
        else if(g_strcmp0(text,LABEL_SHOW) == 0)
            g_vcd_conf->is_show_topbar = TRUE;
        else if(g_strcmp0(text,LABEL_HIDE) == 0)
            g_vcd_conf->is_show_topbar = FALSE;
    }
}

static void set_default_active(StTopbar* context){

    //default is top position
    gtk_toggle_button_set_active((GtkToggleButton *)context->radio1,TRUE);

    if(g_vcd_conf){
        TopbarPostion p = g_vcd_conf->topbar_position;
        if(p == POSITION_LEFT)
            gtk_toggle_button_set_active((GtkToggleButton *)context->radio2,TRUE);
        else if(p == POSITION_RIGHT)
            gtk_toggle_button_set_active((GtkToggleButton *)context->radio3,TRUE);
        else
            gtk_toggle_button_set_active((GtkToggleButton *)context->radio1,TRUE);

        gboolean is_show_topbar = g_vcd_conf->is_show_topbar;
        if(is_show_topbar)
            gtk_toggle_button_set_active((GtkToggleButton *)context->radio4,TRUE);
        else
            gtk_toggle_button_set_active((GtkToggleButton *)context->radio5,TRUE);
    }
}

GtkWidget *vcd_other_topbar_new(void)
{
    StTopbar  context = {0};
    GtkWidget *widget = gtk_fixed_new();
    GtkWidget *label_location = gtk_label_new(LABEL_POSITION);

    GtkWidget *radio1 = gtk_radio_button_new_with_label(NULL,LABEL_TOP);
    GtkWidget *radio2 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(radio1),LABEL_LEFT);
    GtkWidget* radio3 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(radio2),LABEL_RIGHT);
    gtk_fixed_put((GtkFixed *)widget, label_location,  100, 60);
    gtk_fixed_put((GtkFixed *)widget, radio1,  180, 60);
    gtk_fixed_put((GtkFixed *)widget, radio2,  260, 60);
    gtk_fixed_put((GtkFixed *)widget, radio3,  340, 60);
    set_css(label_location, "label{font-size: large;color:#000000}");

    // topbar show/hide
    GtkWidget *radio_show = gtk_radio_button_new_with_label(NULL,LABEL_SHOW);
    GtkWidget *radio_hide = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(radio_show),LABEL_HIDE);
    gtk_fixed_put((GtkFixed *)widget, radio_show,  180, 100);
    gtk_fixed_put((GtkFixed *)widget, radio_hide,  260, 100);

    // tips
    GtkWidget* tips0 = gtk_label_new(LABEL_TIPS_SHOW_HIDE0);
    GtkWidget* tips1 = gtk_label_new(LABEL_TIPS_SHOW_HIDE1);
    gtk_fixed_put((GtkFixed *)widget, tips0,  80, 390);
    gtk_fixed_put((GtkFixed *)widget, tips1,  140, 390);
    set_css(tips0, "label{font-size: small;color:#B5B5B5}");//8B636C
    set_css(tips1, "label{font-size: small;color:#B5B5B5}");

    context.radio1 = radio1;
    context.radio2 = radio2;
    context.radio3 = radio3;
    context.radio4 = radio_show;
    context.radio5 = radio_hide;

    set_default_active(&context);

    g_signal_connect(G_OBJECT(radio1),"toggled", G_CALLBACK(btn_toggled), &context);
    g_signal_connect(G_OBJECT(radio2),"toggled", G_CALLBACK(btn_toggled), &context);
    g_signal_connect(G_OBJECT(radio3),"toggled", G_CALLBACK(btn_toggled), &context);
    g_signal_connect(G_OBJECT(radio_show),"toggled", G_CALLBACK(btn_toggled), &context);
    g_signal_connect(G_OBJECT(radio_hide),"toggled", G_CALLBACK(btn_toggled), &context);

    return widget;
}
